# -*- coding:utf-8 -*-

from com.zzh.utils.dbUtils import MysqlUtils
from com.zzh.utils.table2JSON import Table2Json


class DepartDao(object):
    def __init__(self):
        self.conn = MysqlUtils.get_mysql_connections()

    def add_depart(self, indus_info):
        depart_name = indus_info['depart_name']
        depart_vid = indus_info['depart_vid']
        depart_code = indus_info['depart_code']
        depart_status = indus_info['depart_status']
        depart_sort = indus_info['depart_sort']

        cur = self.conn.cursor()
        sql = "insert into zzh_depart (depart_name,depart_vid,depart_code,depart_status,depart_sort) " \
              "VALUES (%s,%s,%s,%s,%s)"
        result = cur.execute(sql, [depart_name, depart_vid, depart_code, depart_status, depart_sort])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return result

    def update_depart(self, depart_id, indus_info):
        depart_name = indus_info['depart_name']
        depart_vid = indus_info['depart_vid']
        depart_code = indus_info['depart_code']
        depart_status = indus_info['depart_status']
        depart_sort = indus_info['depart_sort']

        cur = self.conn.cursor()
        sql = "update zzh_depart set depart_name=%s,depart_vid=%s,depart_code=%s,depart_status=%s,depart_sort=%s " \
              "WHERE depart_id=%s "
        result = cur.execute(sql, [depart_name, depart_vid, depart_code, depart_status, depart_sort, depart_id])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return result

    def query_depart(self):
        cur = self.conn.cursor()

        sql = "select * from zzh_depart"
        rs = cur.execute(sql)
        depart_list = cur.fetchall()
        depart_json = Table2Json.depart_list_to_json(depart_list)
        cur.close()
        self.conn.close()
        return depart_json


